package com.sl.u_push_plugin

import android.content.Intent
import io.flutter.plugin.common.EventChannel
import io.flutter.plugin.common.MethodChannel

class MessageManager private constructor() {
    var events: EventChannel.EventSink? = null
    var messageMap: MutableMap<String, Any>?= null

    companion object {
        val instance by lazy {
            MessageManager()
        }
    }

    fun initEvents(events: EventChannel.EventSink?) {
        this.events = events
    }

    fun setIntentJson(intent: Intent) {

        if (events == null) {
            var result = ResaultData().Builder()
            intent.extras?.run {
                val keySet: Set<String> = keySet()
                keySet.forEach {
                    result.initData(it, getString(it))
                }
                result.initType(TypeData.message)
                messageMap = result.build()
            }
        } else {
            var result = ResaultData().Builder()
            intent.extras?.run {
                val keySet: Set<String> = keySet()
                keySet.forEach {
                    result.initData(it, getString(it))
                }
                result.initType(TypeData.message)
                events?.success(result.build())
            }
        }
    }

    fun getCacheMessage(result: MethodChannel.Result?) {
        result?.success(messageMap ?: mapOf<String,String>())
        messageMap?.clear()
    }

    fun receiveMessageListener() {

    }

    fun sendJsonMessage2Flutter(json: String) {
        events?.success(json)
    }

}